import xml.etree.ElementTree as et

class DataMgr():
    m_Path = None
    m_Doc = None
    MenuList = []
    def __init__(self, path="./Data.xml"):
        self.m_Path = path

    def LoadXml(self, path="./Data.xml"):
        if (path != self.m_Path):
            self.m_Path = path
        self.m_Doc = et.parse(self.m_Path)

        if self.m_Doc!=None:
            lst = self.m_Doc.getroot().findall("Menu")
            for item in lst:

                menuID = item.attrib.get("MenuId")
                menuName = item.attrib.get("MenuName")
                hasChild = item.attrib.get("HasChild")

                menu = MyMenu()
                menu.MenuId = menuID
                menu.MenuName = menuName
                menu.HasChild = hasChild

                if (hasChild):
                    lstProto = self.GetProtoListByMenuName(menuID)
                    if (lstProto != None):
                        menu.ProtoList = menu.ProtoList + lstProto

                self.MenuList.append(menu)

    def GetProtoListByMenuName(self, menuID):
        index = 0
        xe, index = self.GetXElementByID(menuID)

        if (xe == None): return None
        lst = xe.findall("Proto")
        if (lst==None): return None
        retList = []
        for item in lst:
            protoID = item.attrib.get("ProtoID")
            protoCode = item.attrib.get("ProtoCode")
            protoEnName = item.attrib.get("ProtoEnName")
            protoCnName = item.attrib.get("ProtoCnName")
            protoDesc = item.attrib.get("ProtoDesc")

            proto = MyProto()
            proto.ProtoID = protoID
            proto.ProtoCode = protoCode
            proto.ProtoEnName = protoEnName
            proto.ProtoCnName = protoCnName
            proto.ProtoDesc = protoDesc

            proto.MenuID = menuID
            retList.append(proto)
        return retList

    # region 数据表操作
    def GetProtoArrtList( self, protoID, menuID):

            protoIndex = 0
            xe, protoIndex = self.GetProtoElement(protoID, menuID)

            lst = xe.findall("Attr")

            retLst = []

            if (lst != None and len(lst) > 0):

                for item in lst:
                    protoAttr = MyProtoAttr()

                    protoAttr.AttID = item.attrib.get("AttID")
                    protoAttr.AttType = item.attrib.get("AttType")
                    protoAttr.AttEnName = item.attrib.get("AttEnName")
                    protoAttr.AttCnName = item.attrib.get("AttCnName")
                    protoAttr.AttIsLoop = item.attrib.get("AttIsLoop")
                    protoAttr.AttToLoop = item.attrib.get("AttToLoop")
                    protoAttr.AttToBool = item.attrib.get("AttToBool")
                    protoAttr.AttToBoolResult = item.attrib.get("AttToBoolResult")
                    protoAttr.AttToCus = item.attrib.get("AttToCus")

                    retLst.append(protoAttr)

            return retLst
    # endregion

    # region 查找节点
    def GetXElementByID(self, menuID):
            index = 0
            lst = self.m_Doc.getroot().findall("Menu")
            i = 0
            for item in lst:
                if item.attrib.get("MenuId") == menuID:
                    index = i
                    return item, index
                i=i+1
            return None, index

    def GetXElementByIndex(self, index):
        lst = self.m_Doc.getroot().findall("Menu")
        return lst[index]

    def GetProtoElement(self, protoID, menuID):
        protoIndex = 0
        xe, index = self.GetXElementByID(menuID)
        lst = xe.findall("Proto")

        i=0
        for item in lst:
            if (item.attrib.get("ProtoID") == protoID):
                protoIndex = i
                return item, protoIndex
            i = i+1

        return None, protoIndex

    def GetProtoElementByIndex(self,menuID, index):
        xe, menuIndex = self.GetXElementByID(menuID)
        lst = xe.findall("Proto")
        return lst[index]
    # endregion

instance = DataMgr()


class NodeTag:
    IsMenu=None
    Menu=None
    Proto=None


class MyMenu:
    MenuId=None
    MenuName=None
    HasChild=None
    ProtoList = []


class MyProto:
    ProtoID=None
    ProtoCode=None
    ProtoEnName=None
    ProtoCnName=None
    ProtoDesc=None
    MenuID=None

class MyProtoAttr:
    AttID=None
    AttType=None
    AttEnName=None
    AttCnName=None
    AttIsLoop=None
    AttToLoop=None
    AttToBool=None
    AttToBoolResult=None
    AttToCus=None
    AttIsUsed=None

if __name__ == '__main__':
    instance.LoadXml()